package com.rzjm.exercise.module.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rzjm.exercise.common.base.data.DataTableLayUI;
import com.rzjm.exercise.common.base.service.impl.BaseServiceImpl;
import com.rzjm.exercise.common.exception.TransactionalException;
import com.rzjm.exercise.module.dao.ShopDao;
import com.rzjm.exercise.module.dao.WxUserDao;
import com.rzjm.exercise.module.domain.EventsSection;
import com.rzjm.exercise.module.domain.Shop;
import com.rzjm.exercise.module.domain.WxUser;
import com.rzjm.exercise.module.service.ShopService;
import com.rzjm.exercise.sys.system.user.dao.UserDao;
import com.rzjm.exercise.sys.system.user.domain.UserDO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


@Service
public class ShopServiceImpl extends BaseServiceImpl<ShopDao, Shop> implements ShopService {
    @Resource
    private ShopDao shopDao;
    @Resource
    private UserDao userDao;
    @Resource
    private WxUserDao wxUserDao;

    @Override
    public List<Shop> getPageByUserId(Page<Shop> page, Map<String, Object> map) {
        return shopDao.getPageByUserId(page, map);
    }

    @Override
    public List<Shop> getPageByUserId(Map<String, Object> map) {
        return shopDao.getPageByUserId(map);
    }

    @Override
    public int remove(Integer id) throws TransactionalException {
        try{
            int ret = shopDao.deleteById(id);
            if(ret <= 0){
                throw  new TransactionalException("delete shop remove");
            }

            List<UserDO> sysUsers = userDao.selectListByColumn("shop_id",id);
            sysUsers.forEach(user->{
                user.setShopId(0);
                int ret1 = userDao.updateById(user);
                if(ret1 <= 0){
                    throw  new TransactionalException("delete userDao updateById");
                }
            });

            List<WxUser> wxUsers = wxUserDao.selectListByColumn("shop_id",id);
            wxUsers.forEach(user->{
                user.setShopId(0);
                int ret1 = wxUserDao.updateById(user);
                if(ret1 <= 0){
                    throw  new TransactionalException("delete wxUserDao updateById");
                }
            });
            return ret;
        }catch (Exception ex){
            throw  new TransactionalException("Shop remove  system error");
        }
    }

    @Override
    public List<Shop> getPageByAdminUserId(Page<Shop> page, Map<String, Object> map) {
        return shopDao.getPageByAdminUserId(page, map);
    }

    @Override
    public List<Shop> getPageByAdminUserId(Map<String, Object> map) {
        return shopDao.getPageByAdminUserId(map);
    }

    @Override
    public Shop getDeviceCountByShopId(Integer shopId) {
        return shopDao.getDeviceCountByShopId(shopId);
    }

    @Override
    public Double getDeviceAvgDataByTime(Map<String, Object> temperatureMap) {
        return shopDao.getDeviceAvgDataByTime(temperatureMap);
    }

    @Override
    public List<Shop> getShopListByWenduValue(Page<Shop> page ,Map<String, Object> map) {
        return shopDao.getShopListByWenduValue(page,map);
    }

    @Override
    public DataTableLayUI<Shop> getShopDeviceCountListPage(DataTableLayUI<Shop> condition) {
        Page<Shop> page = new Page<>(condition.getPage(), condition.getLimit());
        List<Shop> list = shopDao.getShopDeviceCountListPage(page, condition.getSearchParams());
        condition.setCount(page.getTotal());
        condition.setData(list);
        condition.setTotalPages(page.getPages());
        condition.setCode(0);
        condition.setMsg("success");
        return condition;
    }
}
